//
//  InviteActView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/10/21.
//

import SwiftUI

//邀请活动
struct InviteActView: View {
    @StateObject var viewModel = InviteActViewModel()
    @EnvironmentObject var userManager: UserManager //加这个才能刷新
    
    var body: some View {
        VStack(spacing: 10) {
            //头部
            VStack(alignment: .leading, spacing: 5) {
                Text("我的邀请码：\(userManager.getUser()?.inviteCode ?? "")")
                    .foregroundColor(.bgItemContent)
                    .font(.system(size: 14))
                Text("邀请人数：\(userManager.getUser()?.inviteCount ?? 0)")
                    .foregroundColor(.bgItemContent)
                    .font(.system(size: 14))
                Text("邀请越到奖励越多，对方充值都能获取到奖励。")
                    .foregroundColor(.bgItemContent2)
                    .font(.system(size: 12))
            }.frame(maxWidth: .infinity, alignment: .leading)
                .padding(15)
                .background(.bgItem)
                .cornerRadius(15)
                .padding(.horizontal, 20)
                .padding(.top, 20)
                .onAppear {
                    viewModel.getData(true)
                }
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
                    .frame(maxWidth: .infinity,maxHeight: .infinity)
            } else {
                ScrollView {
                    if viewModel.statusTitle.isEmpty {
                        Spacer(minLength: 10)
                    } else {
                        Text(viewModel.statusTitle)
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.bgNormalText)
                            .padding(.top,20)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                    LazyVStack(alignment: .leading, spacing: 20) {
                        ForEach(Array(viewModel.items.enumerated()), id: \.element.objectId) { index, item in
                            InviteActItemView(bean: item,viewModel: viewModel)
                        }
                        LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                            viewModel.getData(false)
                        }) {
                            LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                        }
                        .noMore(viewModel.noMore)
                        .preload(offset: 10)
                    }
                }
                .enableRefresh()
                .scrollIndicators(.hidden)
                .padding(.leading, 20)
                .padding(.trailing,15)
            }
        }
        .navigationTitle("邀请活动")
        .toolbar {
            // 添加一个右标题按钮
            ToolbarItem(placement: .navigationBarTrailing) {
                if ((userManager.getUser()?.inviteUserId ?? "").isEmpty == true) {
                    Text("绑定邀请人")
                        .foregroundColor(.bgNormalText)
                        .font(.system(size: 12))
                        .padding(.horizontal,10)
                        .padding(.vertical,7)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showEditDialog(title: "请输邀请码，绑定后无法修改",hint: "请输邀请码", content:  "") { it in
                                viewModel.doBindInvite(code: it ?? "")
                            }
                        }
                }
            }
        }
        .background(.bg)
        .onAppear{
            if !viewModel.isInit {
                viewModel.doCreateInviteCode()
                viewModel.isInit = true
            }
        }
    }
}

#Preview {
    InviteActView()
}
